﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WarOfDev.EF;
using WarOfDev.Web.ViewModels;
using WarOfDev.Web.Models;
using WarOfDev.Core.Common;

namespace WarOfDev.Web
{
    public static class TaskMapping
    {
        public static Task ToModel(this TaskViewModel vm)
        {
            Task task = new Task();
            task.Id = vm.Id;
            task.Title = vm.Title;
            task.Description = vm.Description;

            AssignedTask assignment = new AssignedTask();
            if (vm.DeveloperId == "-1") vm.DeveloperId = string.Empty;

            assignment.DevId = vm.DeveloperId;
            assignment.CreatedOn = assignment.ModifiedOn = DateTime.Now;
            assignment.Status = string.IsNullOrEmpty(vm.DeveloperId) ? (int)EnumTaskStatus.Propose : (int)EnumTaskStatus.Active;
            task.Status = assignment.Status;
            task.ExperienceValue = vm.ExperienceValue;

            task.Assignment.Add(assignment);

            if (string.IsNullOrEmpty(task.Id))
            {
                task.Id = Guid.NewGuid().ToString();
                task.CreatedOn = DateTime.Now;
                assignment.Id = Guid.NewGuid().ToString();
            }
            task.ModifiedOn = DateTime.Now;
            return task;
        }
    }
}